import axios from 'axios';
import {push} from 'react-router-redux';
import {Spin,Icon} from 'antd';

export function AddDatas(form){
    return (dispatch)=>{
          dispatch({type:'add_datas',form:form});
    }
}
export function SetStatus(){
    return (dispatch)=>{
        dispatch({type:'set_status'})
    }
}
export function EditDatas(form){
    return (dispatch)=>{
         dispatch({type:'edit_datas',form:form});
    }
}
export function BatchSubmit(list){
    console.log(list);
    return (dispatch) =>{
        return axios.post('http://101.132.191.91:6011/api/batch',{
            BatchList:list
        }).then((data) => {
            if(typeof(data)=='string'){
                data=JSON.parse(data);
            }else{
                data=data;
            }
            if(1 == data.State){
                console.log('批量导入完成');
                let pagination={};
                    pagination.current = 1,
                    pagination.pageSize = 10;
                dispatch(GetData(pagination));
            }else{
                console.log(data.Message);
            }
        })
    }
}
export function EditData(form,id){
    return (dispatch)=>{
         dispatch({type:'get_load'});
      return  axios.put('http://101.132.191.91:6011/api/user/'+id,{
            UserId:form.id,
            UserName:form.username,
            UserGender:form.sex,
            UserTelPhone:form.telephone
        }).then((data)=>{
            dispatch({type:'load_item'});
            if(typeof(data)=='string'){
                data=JSON.parse(data);
            }else{
                data=data;
            }
            if(data.State == 1){
                let pagination={};
                pagination.current = 1,
                pagination.pageSize = 10,
                dispatch(GetData(pagination));
                console.log('修改成功');
            }else{
                console.log(data.Message);
            }
        })
    }
}
export function GetData(pagination){
    return (dispatch)=>{
        dispatch({type:'get_load'});
        return axios.get('http://101.132.191.91:6011/api/user?pageindex='+pagination.current+'&pagesize='+pagination.pageSize)
                .then((response)=>{
                let data = response.data;
                if(typeof(data.Message)=='string'){
                    data=JSON.parse(data.Message);
                }else{
                    data=data.Message;
                }
                pagination.total=data.dataCount;
                pagination.current =data.pageIndex;
                pagination.pageSize =10;
                let d=[];
                let list=data.list;
                for(let i=0;i<list.length;i++){
                     d.push({
                        Id: list[i].ID,
                        UserId: list[i].UserId,
                        UserName: list[i].UserName,
                        UserGender:list[i].UserGender,
                        UserTelPhone: list[i].UserTelPhone
                    });
                }
                dispatch({type:'init_Data',data:d,pagination:pagination});

            });
    }
}

export function SubmitFile(list){
    return (dispatch) => {
        return fetch('http://10.10.101.196:6015/api/Upload',{
            method: 'POST',
            headers: {
                'enctype': 'multipart/form-data'
            },
            body: list,
        }).then((req) => {
            if(req.ok){
                return req.json();
            }else{
                console.log('error');
            }
        }).then((data) => {
            if(-1 == data.State){
                console.log(data.Message);
            }else{
                console.log(data.Message);
            }
        })
    }
}